// 导入依赖的模块
yepjs.require("jquery");
yepjs.require("YBA.Common");
yepjs.require("YBA.Plugin.plugins");
yepjs.require("YBA.Data.login");

// 登录模块
yepjs.define("forgetPwd", {

    NS: "YBA.Logic",

    field: function() {

        var Biz = {
            // 初始化事件
            init: function() {
                for (var i in Biz.event) {
                    Biz.event[i]();
                }
                Biz.el.jphone.html(Biz.fn.GetQueryString("mobile"));

                // 处理在按钮禁用的情况下，用户刷新页面导致部分浏览器下按钮保持禁用的问题
                Biz.el.jsubmit.removeAttr("disabled");
            },
            //  公共变量
            vars: {
            	// 模板列表
                tpl: (function(){
                    var tpl = {};
                    $("script[type='text/ysmart']").each(function(){
                        var t = $(this);
                        tpl[t.attr("data-name")] = t.html();
                    });
                    return tpl;
                })(),
                //记录定时器
                timer:""
            },
            // 页面元素
            el: {
                jform: $("#j-form"),
                jsubmit: $("#j-submit"),
                jphone:$("#j-phone"),
                jgetCode:$(".j-getCode")
            },
            // 功能函数集合
            fn: {
            	//获取地址栏参数
                GetQueryString:function (parameterName){
                     var reg = new RegExp("(^|&)"+ parameterName +"=([^&]*)(&|$)");
                     var r = window.location.search.substr(1).match(reg);
                     if(r!=null)return  unescape(r[2]); return null;
                },
                // 表单验证
                formCheck: function(form,name) {

                	// 显示错误信息
                    var showError = function(obj, msg) {
                        var msgDom = obj.siblings('.message');
                        var msgTxt = msgDom.children('i')
                        msgDom.addClass("msg-error").removeClass('msg-tip');
                        msgTxt.html(msg);
                        msgDom.show();
                    };

                    // 隐藏错误信息
                    var hideError = function(obj, msg) {
                        var msgDom = obj.siblings('.message');
                        var msgTxt = msgDom.children('i')
                        msgDom.hide();
                        msgDom.addClass("msg-tip").removeClass('msg-error');
                        msgTxt.html(msg);
                    };

                    // 数据验证对象
                    var dataCheck = {
                        newPassword:{
                            obj: form.find("[name='newPassword']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入新密码");
                                    return false;
                                }else if(!YBA.Common.check.isPwd(val)){
                                	showError(this.obj,"至少六位的英文数字组合");
                                    return false;
                                }else if(YBA.Common.check.isHowLong(val,50)){
                                	showError(this.obj,"新密码过长");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入新密码");
                                    return true;
                                }
                            }
                        },
                        passwordSecond: {
                            obj: form.find("[name='passwordSecond']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请再次输入新密码");
                                    return false;
                                } else if(val != dataCheck["newPassword"].obj.val()){
                                    showError(this.obj,"两次输入的密码不一致");
                                    return false;
                                }else {
                                    hideError(this.obj,"请再次输入新密码");
                                    return true;
                                }
                            }
                        },
                        code: {
                            obj: form.find("[name='code']"),
                            fn: function() {
                            	if(this.obj.length == 0){
                            		return true;
                            	}
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入验证码");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入验证码");
                                    return true;
                                }
                            }
                        }
                    };

                    if (name && dataCheck[name]) {
                        return dataCheck[name].fn();
                    } else {
                        var errLen = 0;
                        for (var i in dataCheck) {
                            if (!dataCheck[i].fn()) {
                                errLen++;
                            }
                        }
                        if (errLen > 0) {
                            return false;
                        }
                        return true;
                    }
                },
                // 表单提交
                submit: function() {
                    if (Biz.fn.formCheck(Biz.el.jform)) {
                        Biz.el.jsubmit.attr("disabled", "disabled");
                        var data = Biz.el.jform.serialize();
                        data = data + "&username="+ Biz.fn.GetQueryString("username") +"&token=tokennawang"+"&type=1"+"&userId="+ Biz.fn.GetQueryString("userId");
                        YBA.Data.login.oncePasswordUpdate(data, function(json){
                            if(json.code == 200){
                            	YBA.Plugin.msgtip({type: "o", msg: json.desc});
                            	setTimeout(function(){
                                    location.href = "/ebeim-api/login.jsp";
                                }, 1500);
                            }else{
                                Biz.el.jsubmit.removeAttr("disabled");
                                YBA.Plugin.msgtip({type: "i",msg: json.desc});
                            }
                        })
                    }
                    return false;
                },
                //短信发送倒计时
                countDown:function(dom,time){
                	dom.val(time+"秒");
                	Biz.vars.timer = setInterval(function(){
                		if(time>0){
                			time--;
	                		dom.val(time+"秒");
                		}else{
                			clearInterval(Biz.vars.timer);
                			dom.removeAttr("disabled").removeClass("disableBtn").addClass("codeBtn").val("发送验证码");
                		}
                	},1000);
                }
            },
            // 初始化时执行的所有事件
            event: {
                // 表单提交
                submit: function() {
                    Biz.el.jform.bind("submit", Biz.fn.submit);
                },
                // 文本框验证
                blurCheck: function() {
                    Biz.el.jform.find(".required").bind("blur", function() {
                        var t = $(this);
                        var name = t.attr("name");
                        Biz.fn.formCheck(Biz.el.jform,name);
                    });
                },
                //获取短信验证码
                getMessageCode:function(){
                	Biz.el.jgetCode.bind("click",function(){
                		var data = {
                			token:"tokennawang",
                			username: Biz.fn.GetQueryString("username")	
                		}
                		var t = $(this);
                    	t.attr("disabled", "disabled").removeClass("codeBtn").addClass("disableBtn");
                    	Biz.fn.countDown(t,120);
                		YBA.Data.login.getMessageCode(data,function(json){
                			if(json.code == 200) {
                				YBA.Plugin.msgtip({type: "o", msg: json.desc});
                			}else{
                                YBA.Plugin.msgtip({type: "i", msg: json.desc});
                            }
                		})	
                	})
                }
            }
        };

        Biz.init();

        return Biz;
    }
});
